package com.thinkfit.jy.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.common.lib.bean.DeviceInfoBean;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.manager.DataManager;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.jaygoo.widget.OnRangeChangedListener;
import com.jaygoo.widget.RangeSeekBar;
import com.thinkfit.jy.R;
import com.thinkfit.jy.bean.CreateTrainItemBean;
import com.thinkfit.jy.utils.BluetoothOrder;
import com.thinkfit.jy.utils.LogUtils;
import com.thinkfit.jy.utils.SerialPortUtils;
import com.thinkfit.jy.utils.ToastUtils;
import com.thinkfit.jy.utils.Utils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class TrainGeneralSituationPopupWindow extends PopupWindow implements View.OnClickListener , PopupWindow.OnDismissListener {

    private Context mContext;

    private View view;
    TextView tvSize,tvTimeSize;
    ConstraintLayout clBg;
    BarChart bc1;
    List<CreateTrainItemBean> brustlist;
    int time = 0;


    public TrainGeneralSituationPopupWindow(Context context, List<CreateTrainItemBean> list){
        mContext = context;
        brustlist = list;
        view = LayoutInflater.from(context).inflate(R.layout.popupwindow_train_general_situation, null);

        clBg = view.findViewById(R.id.cl_bg);
        bc1 = view.findViewById(R.id.bc_1);
        tvSize = view.findViewById(R.id.tv_size);
        tvTimeSize = view.findViewById(R.id.tv_time_size);
        if(list != null && list.size()>0){

            for (CreateTrainItemBean bean :list){
                time = time + Integer.parseInt(bean.getTime());
            }
            if(DataManager.Companion.getInstance().getLanguage() == 1){
                tvSize.setText("Sections : "+brustlist.size());
                if(time > 1){
                    tvTimeSize.setText("Duration : "+time+" mins");
                }else {
                    tvTimeSize.setText("Duration : "+time+" min");
                }

            }else {
                tvSize.setText("训练总段数 : "+brustlist.size()+" 段");
                tvTimeSize.setText("训练总时长 : "+time+" 分钟");
            }
        }

        initBarCher();

        initBarCherData(list);

        clBg.setOnClickListener(this);


        this.setContentView(view);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();

        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.parseColor("#4D000000"));
//        ColorDrawable dw = new ColorDrawable(Color.parseColor("#ffffff"));
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(this);

    }

    public void show(Activity context) {
        showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.cl_bg:
                dismiss();
                 break;
        }
    }

    public void initBarCher(){
        bc1.getLegend().setEnabled(false);//不显示图例
        bc1.getDescription().setEnabled(false);//不显示描述

        XAxis xAxis = bc1.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//x轴所在位置
        xAxis.setAxisMinimum(0);//x轴最小值
        xAxis.setCenterAxisLabels(true);//x轴显示标签居中
        xAxis.setTextColor(mContext.getColor(com.common.lib.R.color.transparent));
        xAxis.setDrawGridLines(true);
        xAxis.setDrawAxisLine(true);
        xAxis.setGridColor(mContext.getColor(R.color.color_3A3A3A));


        YAxis axisLeft = bc1.getAxisLeft();
        axisLeft.setDrawAxisLine(false);
        axisLeft.setDrawGridLines(false);
        bc1.getAxisRight().setDrawGridLines(false);
        axisLeft.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        axisLeft.setAxisMinimum(0f);
        axisLeft.setTextColor(mContext.getColor(R.color.white));



//        bc1.setTouchEnabled(false);
        bc1.getAxisRight().setDrawGridLines(false);
        bc1.getAxisRight().setEnabled(false);



        Description description=new Description();
        description.setEnabled(false);
        bc1.setDescription(description);


    }

    public void initBarCherData(List<CreateTrainItemBean> brustlist){
        if(brustlist == null || brustlist.size() == 0){
            return;
        }
        LogUtils.getInstance().l(LogUtils.HINT,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx   brustlist.size() : "+brustlist.size());
        ArrayList<BarEntry> slops = new ArrayList<>();
        ArrayList<BarEntry> speeds = new ArrayList<>();
        for (int i = 0; i < brustlist.size(); i++){
            slops.add(new BarEntry(i+1, Float.parseFloat(brustlist.get(i).getSlop()+"")));
            speeds.add(new BarEntry(i+1, Float.parseFloat(brustlist.get(i).getSpeed()+"")));
            LogUtils.getInstance().l(LogUtils.HINT,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx   slop : "+brustlist.get(i).getSlop()+"   speed : "+brustlist.get(i).getSpeed());
        }
        BarDataSet slope = new BarDataSet(slops,mContext.getString(R.string.app_slop));
        BarDataSet speeds1 = new BarDataSet(speeds, mContext.getString(R.string.app_speed_txt));
        speeds1.setColor(mContext.getColor(R.color.color_4865B5));
//        speeds1.setBarBorderColor(getColor(R.color.color_4865B5));
        slope.setColor(mContext.getColor(R.color.color_3BC2AC));
//        slope.setBarBorderColor(getColor(R.color.color_3BC2AC));
        speeds1.setDrawValues(false);
        slope.setDrawValues(false);
        ;

//        BarData barData = new BarData(data());
        BarData barData = new BarData(speeds1,slope);
        bc1.setData(barData);


        float barspace = 0.2f;
        float groupspace = 0.2f;
        float barwidth = 0.2f;
        float granularity =(barwidth + barspace)*2 + groupspace;
        LogUtils.getInstance().l(LogUtils.HINT,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx   granularity : "+granularity);
        bc1.setScaleEnabled(false);
        bc1.getXAxis().setGranularity(1.0f);
        bc1.setScaleX(granularity);

        bc1.groupBars(1.2f,groupspace,-0.6f);

        barData.setBarWidth(barwidth);

        bc1.invalidate();
    }

    @Override
    public void onDismiss() {

    }
}
